import {MicroAppModule} from "./MicroAppModule";
import {reactive} from "@peryl/react-compose";
import {uuid} from "@peryl/utils/uuid";
import {iMicroPageMeta} from "@peryl/micro-web";

export const microAppNav = MicroAppModule.createRegistration((microApp) => {
  /**
   * 创建页面所使用的nav对象
   * @author  韦胜健
   * @date    2024/1/31 14:21
   */
  microApp.createPageNav = (pageMeta: iMicroPageMeta) => {

    /*获取页面所属的tabMeta*/
    const getTabMeta = () => microApp.state.tabs.find(i => !!i.pages.find(p => p.id === pageMeta.id));

    const nav = {
      /**
       * 在所属的tabMeta下打开一个新页面
       * @author  韦胜健
       * @date    2024/1/31 14:22
       */
      push: (path: string, data?: any) => {
        const tabMeta = getTabMeta();
        if (!tabMeta) {return;}
        microApp.pushPage(tabMeta, { path, data });
      },
      /**
       * 在所属的tabMeta下回退页面
       * @author  韦胜健
       * @date    2024/1/31 14:22
       * @param   backNum     回退页面的数量
       * @param   backData    回退页面参数，回退之后停留的页面可以通过onBack钩子函数监听页面回退动作
       */
      back: (backNum = 1, backData?: any) => {
        const tabMeta = getTabMeta();
        if (!tabMeta) {return;}
        const pageIndex = tabMeta.pages.findIndex(i => i.id === pageMeta.id);
        if (pageIndex <= 0) {return;}
        let leftPages = tabMeta.pages.slice(0, pageIndex);
        while (backNum > 0 && leftPages.length > 1) {
          leftPages.pop();
          backNum--;
        }
        tabMeta.pages = leftPages;
        const leftLastPage = leftPages[leftPages.length - 1];
        microApp.hooks.onHandleBackData.exec({ handlePageMeta: leftLastPage, backData });
        microApp.showPage(leftLastPage);
      },
      /**
       * 回退所有页面
       * @author  韦胜健
       * @date    2024/1/31 14:25
       */
      backoff: (backData?: any) => {
        const tabMeta = getTabMeta();
        if (!tabMeta) {return;}
        nav.back(tabMeta.pages.length - 1, backData);
      },
      /**
       * 重定向页面
       * @author  韦胜健
       * @date    2024/1/31 14:25
       */
      redirect: (path: string, data?: any) => {
        const tabMeta = getTabMeta();
        if (!tabMeta) {return;}
        tabMeta.pages.pop();
        microApp.pushPage(tabMeta, { path, data });
      },
      /**
       * 刷新页面
       * @author  韦胜健
       * @date    2024/1/31 14:37
       */
      refreshPage: () => {
        const tabMeta = getTabMeta();
        if (!tabMeta) {return;}
        tabMeta.pages.pop();
        microApp.pushPage(tabMeta, { path: pageMeta.path, data: pageMeta.data });
      },
      /**
       * 新窗口打开页面
       * @author  韦胜健
       * @date    2024/1/31 14:37
       */
      openWindow: () => {
        alert('未完成！');
      },

      /**
       * 打开新的tab
       * @author  韦胜健
       * @date    2024/1/31 14:37
       */
      openTab: microApp.openTab,
      /**
       * 关闭当前tab
       * @author  韦胜健
       * @date    2024/1/31 14:37
       */
      closeTab: () => {
        const tabMeta = getTabMeta();
        if (!tabMeta) {return;}
        microApp.closeTab(tabMeta);
      },
      /**
       * 刷新当前tab
       * @author  韦胜健
       * @date    2024/1/31 14:37
       */
      refreshTab: () => {
        const tabMeta = getTabMeta();
        if (!tabMeta) {return;}
        microApp.refreshTab(tabMeta);
      },

      loading: (() => {
        const state = reactive({ loadingCodes: [] as string[] });
        const loading = () => {
          const code = uuid();
          state.loadingCodes.push(code);
          return () => {
            const index = state.loadingCodes.indexOf(code);
            if (index > -1) {state.loadingCodes.splice(index, 1);}
          };
        };
        return Object.assign(loading, {
          state,
          closeAll: () => state.loadingCodes = [],
          isLoading: () => state.loadingCodes.length > 0
        });
      })(),
    };

    return nav;
  };
});
